package com.mentaleye.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.RoleBean;
import com.mentaleye.biz.DictionaryBiz;
import com.mentaleye.biz.PermissionsBiz;
import com.mentaleye.biz.PermissionsRoleBiz;
import com.mentaleye.biz.RoleBiz;
import com.mentaleye.dal.model.PermissionsExample;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.util.CommonPage;

@Controller
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private RoleBiz roleBiz;
	@Autowired private PermissionsBiz permissionBiz;
	@Autowired private PermissionsRoleBiz permissionsRoleBiz;
	@Autowired private DictionaryBiz dictionaryBiz;

	@RequestMapping("/roleList")
	public ModelAndView list() {
		ModelAndView view = new ModelAndView("ss/role/roleList");
		view.addObject("roles", roleBiz.getAllRole());
		return view;
	}

	@RequestMapping("/roleListData")
	@ResponseBody
	public String roleListData(CommonPage commonPage) {
		roleBiz.getRoleBypage(commonPage);
		//System.out.println(commonPage.toSHowPage());
		return commonPage.toSHowPage();
	}

	@RequestMapping("/roleEdit")
	public ModelAndView roleEdit(@RequestParam(value = "roleNo", required = false) Integer roleNo) {
		ModelAndView view = new ModelAndView("ss/role/roleEdit");
		if (roleNo != null) {
			view.addObject("role", roleBiz.getRoleByNo(roleNo));
		}
		return view;
	}

	@RequestMapping("/save")
	public @ResponseBody String save(RoleBean roleBean) {
		return JSONObject.toJSONString(roleBiz.editRole(roleBean));
	}

	@RequestMapping("/delete")
	public @ResponseBody String delete(@RequestParam(value = "roleNo", required = false) Integer id) {
		return JSONObject.toJSONString(roleBiz.dropRole(id));
	}
	
	
	@RequestMapping("/rolePermissionlist")
	public ModelAndView rolePermissionlist(@RequestParam(value = "roleNo", required = false) Integer id) {
		ModelAndView view = new ModelAndView("ss/role/assignPermissions");
		Map<Integer, String> permissionsTypeMap = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.PERMISSIONSTYPE.getCode());
		view.addObject("permissionsTypeMap", permissionsTypeMap);
		view.addObject("permissionsType", JSONObject.toJSONString(permissionsTypeMap));
		view.addObject("roleNo", id);
		view.addObject("permissions", JSONObject.toJSONString(permissionBiz.queryPermissions(new PermissionsExample())));
		view.addObject("rolePermission", JSONObject.toJSONString(permissionsRoleBiz.queryPermissionsRole(id)));
		view.addObject("role", roleBiz.getRoleByNo(id));
		return view;
	}
	
	@RequestMapping(value="/treeData")
	@ResponseBody
	public  String listdata(@RequestParam(value="roleNo",required = false) Integer roleNo){
		return JSONObject.toJSONString(permissionBiz.queryPermissions(new PermissionsExample()));
	}
}